//
//  JXStatusOriginalView.swift
//  01-weiboSwift
//
//  Created by TwistFate on 16/3/2.
//  Copyright © 2016年 TwistFate. All rights reserved.
//

import UIKit
import SnapKit

let JXStatusCellMargin :CGFloat = 8

class JXStatusOriginalView: UIView {
    
    var contentViewConstraint : Constraint?

    var status : JXStatus? {
        didSet{
            
            if let string = status?.user?.profile_image_url {
                let url = NSURL(string: string)
            iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            
            nameLabel.text = status?.user?.screen_name
            
            timeLabel.text = status?.created_at
            
            sourceLabel.text = status?.source
            
            contectlabel.text = status?.text
            
            if let mbrank = status?.user?.mbrank {
                if mbrank>0 && mbrank<=6 {
                    memberView.image = UIImage(named: "common_icon_membership_level\(mbrank)")
                }else{
                    memberView.image = nil
                }
            }
            
            if let verified_type = status?.user?.verified_type {
                var image : UIImage? = nil
                switch verified_type {
                case 0:
                    image = UIImage(named: "avatar_vip")
                case 2,3,5:
                    image = UIImage(named: "avatar_enterprise_vip")
                case 220:
                    image = UIImage(named: "avatar_grassroot")
                default:
                    image = nil
                }
                verifiedView.image = image
            }
            
            contentViewConstraint?.uninstall()
            if let picture = status?.pic_urls?.count where picture > 0 {
                statusPictureView.status = status
                self.statusPictureView.hidden = false
                self.snp_makeConstraints { (make) -> Void in
                    self.contentViewConstraint=make.bottom.equalTo(statusPictureView).offset(JXStatusCellMargin).constraint
                }
            }else{
                self.statusPictureView.hidden = true
                self.snp_makeConstraints { (make) -> Void in
                    self.contentViewConstraint=make.bottom.equalTo(contectlabel).offset(JXStatusCellMargin).constraint
                }
            }
            
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.whiteColor()
        prepareUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 准备UI
    private func prepareUI() {
        // 添加子控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedView)
        addSubview(memberView)
        addSubview(contectlabel)
        addSubview(statusPictureView)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(JXStatusCellMargin)
            make.leading.equalTo(self).offset(JXStatusCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(JXStatusCellMargin)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(JXStatusCellMargin)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.leading.equalTo(timeLabel.snp_trailing).offset(JXStatusCellMargin)
        }
        
        contectlabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(JXStatusCellMargin)
            make.leading.equalTo(self).offset(JXStatusCellMargin)
            make.trailing.equalTo(self).offset(-JXStatusCellMargin)
        }
        
        memberView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(JXStatusCellMargin)
            make.centerY.equalTo(nameLabel)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        
        statusPictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contectlabel.snp_bottom).offset(JXStatusCellMargin)
            make.leading.equalTo(self).offset(JXStatusCellMargin)
            make.size.equalTo(CGSize(width: 180, height: 120))
        }
        
        self.snp_makeConstraints { (make) -> Void in
            self.contentViewConstraint = make.bottom.equalTo(statusPictureView).offset(JXStatusCellMargin).constraint
        }

        
    }

    
    // MARK: - 懒加载
    /// 用户头像
    private lazy var iconView = UIImageView(image: UIImage(named: "avatar"))
    
    /// 用户名称
    private lazy var nameLabel: UILabel = UILabel(fonsize: 14, textColor: UIColor.darkGrayColor())
    
    /// 时间label
    private lazy var timeLabel: UILabel = UILabel(fonsize: 9, textColor: UIColor.orangeColor())
    
    /// 来源
    private lazy var sourceLabel: UILabel = UILabel(fonsize: 9, textColor: UIColor.lightGrayColor())
    
    /// 认证图标
    private lazy var verifiedView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    /// 会员等级
    private lazy var memberView: UIImageView = UIImageView()
    
    private lazy var contectlabel : UILabel = {
        let contect = UILabel ( fonsize: 15, textColor: UIColor.blackColor())
        
        contect.numberOfLines = 0
        
        return contect
        }()
    
    private lazy var statusPictureView : JXStatusPictureView = JXStatusPictureView()
}
